package com.tenebrous.jestic.compents;

import com.tenebrous.jestic.compents.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public Map exceptionHandler(Exception e) {
        log.error("服务异常", e);
        Map<String, Object> map = new HashMap<>();
        map.put("message", e.getMessage());
        map.put("status", 999);
        return map;
    }

    /**
     * 1 内部API调用的异常处理
     */
    @ResponseBody
    @ExceptionHandler(value = BizException.class)
    public Map bizException(BizException e) {
        log.error("服务异常", e);
        Map<String, Object> map = new HashMap<>();
        map.put("message", e.getMessage());
        map.put("status", e.getCode());
        return map;
    }

}
